
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>十五、回溯算法 · 计算机笔记</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="海拉尔的三角">
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search-pro-fixed/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-ancre-navigation/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-code/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-insert-logo-link-website/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-prism/prism-tomorrow.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-flexible-alerts/style.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-lightbox/css/lightbox.min.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-pageview-count/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-auto-scroll-table/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-katex-pro/katex.min.css">
                
            
                
                <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-sectionx/sectionx.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-terminull-light/terminull.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    

        
    
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="monotoneStack_withNum.html" />
    
    
    <link rel="prev" href="dynamicProgram_withNum.html" />
    

    
        <link rel="shortcut icon" href="image/ico.png" type="image/x-icon">
    
    
    
    
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="image/ico.png">
    
    
        
    
    
        
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    
    
        
        <li>
            <a href="https://github.com/spite-triangle" target="_blank" class="custom-link">Github</a>
        </li>
    
    

    
    <li class="divider"></li>
    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    简介
            
                </a>
            

            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../cpp.html">
            
                <a href="../cpp.html">
            
                    
                    C/C++
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1.1" data-path="foundation_withNum.html">
            
                <a href="foundation_withNum.html">
            
                    
                    一、基础概念
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.2" data-path="keyword_withNum.html">
            
                <a href="keyword_withNum.html">
            
                    
                    二、关键字
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3" data-path="point_reference_withNum.html">
            
                <a href="point_reference_withNum.html">
            
                    
                    三、指针与引用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.4" data-path="oop_withNum.html">
            
                <a href="oop_withNum.html">
            
                    
                    四、面向对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.5" data-path="memory_withNum.html">
            
                <a href="memory_withNum.html">
            
                    
                    五、内存
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.6" data-path="process_withNum.html">
            
                <a href="process_withNum.html">
            
                    
                    六、进程与线程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.7" data-path="locks_withNum.html">
            
                <a href="locks_withNum.html">
            
                    
                    七、锁
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.8" data-path="stl_withNum.html">
            
                <a href="stl_withNum.html">
            
                    
                    八、STL
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.9" data-path="dataStruction_withNum.html">
            
                <a href="dataStruction_withNum.html">
            
                    
                    九、数据结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.10" data-path="array_list_withNum.html">
            
                <a href="array_list_withNum.html">
            
                    
                    十、数组链表操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.11" data-path="find_withNum.html">
            
                <a href="find_withNum.html">
            
                    
                    十一、目标查找
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.12" data-path="tree_withNum.html">
            
                <a href="tree_withNum.html">
            
                    
                    十二、树
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.13" data-path="binaryTree_withNum.html">
            
                <a href="binaryTree_withNum.html">
            
                    
                    十三、二叉树
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.14" data-path="dynamicProgram_withNum.html">
            
                <a href="dynamicProgram_withNum.html">
            
                    
                    十四、动态规划
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="2.1.15" data-path="backtracking_withNum.html">
            
                <a href="backtracking_withNum.html">
            
                    
                    十五、回溯算法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.16" data-path="monotoneStack_withNum.html">
            
                <a href="monotoneStack_withNum.html">
            
                    
                    十六、单调栈
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.17" data-path="toollib_withNum.html">
            
                <a href="toollib_withNum.html">
            
                    
                    附录 工具库
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../../MySQL/mysql.html">
            
                <a href="../../MySQL/mysql.html">
            
                    
                    MySQL
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1.1" data-path="../../MySQL/chapter/introduction_withNum.html">
            
                <a href="../../MySQL/chapter/introduction_withNum.html">
            
                    
                    一、简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.2" data-path="../../MySQL/chapter/foundation_withNum.html">
            
                <a href="../../MySQL/chapter/foundation_withNum.html">
            
                    
                    二、控制台操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.3" data-path="../../MySQL/chapter/operatorDB_withNum.html">
            
                <a href="../../MySQL/chapter/operatorDB_withNum.html">
            
                    
                    三、数据库、表操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.4" data-path="../../MySQL/chapter/operatorData_withNum.html">
            
                <a href="../../MySQL/chapter/operatorData_withNum.html">
            
                    
                    四、数据操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.5" data-path="../../MySQL/chapter/transaction_withNum.html">
            
                <a href="../../MySQL/chapter/transaction_withNum.html">
            
                    
                    五、事务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.6" data-path="../../MySQL/chapter/index_withNum.html">
            
                <a href="../../MySQL/chapter/index_withNum.html">
            
                    
                    六、索引
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.7" data-path="../../MySQL/chapter/authority_withNum.html">
            
                <a href="../../MySQL/chapter/authority_withNum.html">
            
                    
                    七、权限管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.8" data-path="../../MySQL/chapter/backup_withNum.html">
            
                <a href="../../MySQL/chapter/backup_withNum.html">
            
                    
                    八、备份
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../../linux/linux.html">
            
                <a href="../../linux/linux.html">
            
                    
                    Linux
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1.1" data-path="../../linux/chapter/foundation_withNum.html">
            
                <a href="../../linux/chapter/foundation_withNum.html">
            
                    
                    一、原理知识
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.2" data-path="../../linux/chapter/command_withNum.html">
            
                <a href="../../linux/chapter/command_withNum.html">
            
                    
                    二、指令
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="5.1" data-path="../../tcp_udp/tcp_upd.html">
            
                <a href="../../tcp_udp/tcp_upd.html">
            
                    
                    网络
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1.1" data-path="../../tcp_udp/chapter/foundation_withNum.html">
            
                <a href="../../tcp_udp/chapter/foundation_withNum.html">
            
                    
                    一、网络通讯基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.1.2" data-path="../../tcp_udp/chapter/tcp_udp_withNum.html">
            
                <a href="../../tcp_udp/chapter/tcp_udp_withNum.html">
            
                    
                    二、tcp/udp
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >十五、回溯算法</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <div id="anchor-navigation-ex-navbar"><i class="fa fa-anchor"></i><ul><li><span class="title-icon "></span><a href="#&#x56DE;&#x6EAF;&#x7B97;&#x6CD5;"><b></b>&#x56DE;&#x6EAF;&#x7B97;&#x6CD5;</a></li><li><span class="title-icon "></span><a href="#1-&#x6846;&#x67B6;"><b></b>1. &#x6846;&#x67B6;</a></li><li><span class="title-icon "></span><a href="#2-&#x5168;&#x6392;&#x5217;"><b></b>2. &#x5168;&#x6392;&#x5217;</a></li><li><span class="title-icon "></span><a href="#3-&#x5168;&#x7687;&#x540E;"><b></b>3. &#x5168;&#x7687;&#x540E;</a></li></ul></div><a href="#&#x56DE;&#x6EAF;&#x7B97;&#x6CD5;" id="anchorNavigationExGoTop"><i class="fa fa-arrow-up"></i></a><h1 style="font-size:60px;text-align:center;" id="&#x56DE;&#x6EAF;&#x7B97;&#x6CD5;"><a name="&#x56DE;&#x6EAF;&#x7B97;&#x6CD5;" class="anchor-navigation-ex-anchor" href="#&#x56DE;&#x6EAF;&#x7B97;&#x6CD5;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x56DE;&#x6EAF;&#x7B97;&#x6CD5;</h1>

<h1 id="1-&#x6846;&#x67B6;"><a name="1-&#x6846;&#x67B6;" class="anchor-navigation-ex-anchor" href="#1-&#x6846;&#x67B6;"><i class="fa fa-link" aria-hidden="true"></i></a>1. &#x6846;&#x67B6;</h1>
<ul>
<li><strong>&#x8DEF;&#x5F84;</strong>&#xFF1A;&#x8BB0;&#x5F55;&#x505A;&#x8FC7;&#x5177;&#x4F53;&#x7684;&#x9009;&#x62E9;</li>
<li><strong>&#x6807;&#x8BB0;</strong>&#xFF1A;&#x6807;&#x8BB0;&#x8BB0;&#x5F55;&#x8FC7;&#x7684;&#x9009;&#x62E9;</li>
<li><strong>&#x9009;&#x62E9;&#x5217;&#x8868;</strong>&#xFF1A;&#x4E5F;&#x5C31;&#x662F;&#x4F60;&#x5F53;&#x524D;&#x53EF;&#x4EE5;&#x505A;&#x7684;&#x9009;&#x62E9;&#x3002;</li>
<li><strong>&#x7ED3;&#x675F;&#x6761;&#x4EF6;</strong>&#xFF1A;&#x4E5F;&#x5C31;&#x662F;&#x5230;&#x8FBE;&#x51B3;&#x7B56;&#x6811;&#x5E95;&#x5C42;&#xFF0C;&#x65E0;&#x6CD5;&#x518D;&#x505A;&#x9009;&#x62E9;&#x7684;&#x6761;&#x4EF6;&#x3002;</li>
</ul>
<pre class="language-"><code class="lang-python">result <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
<span class="token keyword">def</span> <span class="token function">backtrack</span><span class="token punctuation">(</span>&#x8DEF;&#x5F84;<span class="token punctuation">,</span> &#x9009;&#x62E9;&#x5217;&#x8868;<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">if</span> &#x6EE1;&#x8DB3;&#x7ED3;&#x675F;&#x6761;&#x4EF6;<span class="token punctuation">:</span>
        result<span class="token punctuation">.</span>add<span class="token punctuation">(</span>&#x8DEF;&#x5F84;<span class="token punctuation">)</span>
        <span class="token keyword">return</span>

    <span class="token keyword">for</span> &#x9009;&#x62E9; <span class="token keyword">in</span> &#x9009;&#x62E9;&#x5217;&#x8868;<span class="token punctuation">:</span>
        &#x505A;&#x9009;&#x62E9;
        backtrack<span class="token punctuation">(</span>&#x8DEF;&#x5F84;<span class="token punctuation">,</span> &#x9009;&#x62E9;&#x5217;&#x8868;<span class="token punctuation">)</span>
        &#x64A4;&#x9500;&#x9009;&#x62E9;
</code></pre>
<p><strong>&#x5176;&#x6838;&#x5FC3;&#x5C31;&#x662F; for &#x5FAA;&#x73AF;&#x91CC;&#x9762;&#x7684;&#x9012;&#x5F52;&#xFF0C;&#x5728;&#x9012;&#x5F52;&#x8C03;&#x7528;&#x4E4B;&#x524D;&#x300C;&#x505A;&#x9009;&#x62E9;&#x300D;&#xFF0C;&#x5728;&#x9012;&#x5F52;&#x8C03;&#x7528;&#x4E4B;&#x540E;&#x300C;&#x64A4;&#x9500;&#x9009;&#x62E9;&#x300D;&#x3002;</strong></p>
<h1 id="2-&#x5168;&#x6392;&#x5217;"><a name="2-&#x5168;&#x6392;&#x5217;" class="anchor-navigation-ex-anchor" href="#2-&#x5168;&#x6392;&#x5217;"><i class="fa fa-link" aria-hidden="true"></i></a>2. &#x5168;&#x6392;&#x5217;</h1>
<p style="text-align:center;"><a href="../../image/cpp/traverseAll.jpg" data-lightbox="0a686b28-7139-4632-aa5b-eae22d63795a" data-title="undefined"><img src="../../image/cpp/traverseAll.jpg" alt="undefined"></a></p>

<pre class="language-"><code class="lang-cpp"><span class="token keyword">void</span> <span class="token function">printAll</span><span class="token punctuation">(</span><span class="token keyword">const</span> vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span> <span class="token operator">&amp;</span> nums<span class="token punctuation">,</span>vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span> <span class="token operator">&amp;</span>path<span class="token punctuation">,</span>vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span> <span class="token operator">&amp;</span>book<span class="token punctuation">)</span><span class="token punctuation">{</span>

    <span class="token comment">// &#x7ED3;&#x675F;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>path<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> nums<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        string res <span class="token operator">=</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> item<span class="token operator">:</span>path<span class="token punctuation">)</span><span class="token punctuation">{</span>
            res <span class="token operator">=</span> <span class="token function">to_string</span><span class="token punctuation">(</span>item<span class="token punctuation">)</span> <span class="token operator">+</span> res<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        cout <span class="token operator">&lt;&lt;</span> res <span class="token operator">&lt;&lt;</span> endl<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// &#x5217;&#x4E3E;&#x6240;&#x6709;&#x60C5;&#x51B5;</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> nums<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>book<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token boolean">false</span><span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            <span class="token comment">// &#x8FDB;&#x5165;</span>
            path<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span>nums<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            book<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>

            <span class="token function">printAll</span><span class="token punctuation">(</span>nums<span class="token punctuation">,</span>path<span class="token punctuation">,</span>book<span class="token punctuation">)</span><span class="token punctuation">;</span>

            <span class="token comment">// &#x51FA;&#x6765;</span>
            path<span class="token punctuation">.</span><span class="token function">pop_back</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            book<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// &#x7ED3;&#x675F;</span>
<span class="token punctuation">}</span>
</code></pre>
<h1 id="3-&#x5168;&#x7687;&#x540E;"><a name="3-&#x5168;&#x7687;&#x540E;" class="anchor-navigation-ex-anchor" href="#3-&#x5168;&#x7687;&#x540E;"><i class="fa fa-link" aria-hidden="true"></i></a>3. &#x5168;&#x7687;&#x540E;</h1>
<sec data-title="&#x5B9E;&#x73B0;&#x4EE3;&#x7801;" data-id="queue" data-show="true" data-collapse="true"><div class="panel panel-default"><div class="panel-heading"><b>&#x5B9E;&#x73B0;&#x4EE3;&#x7801;<a class="pull-right section atTitle btn btn-default" target="queue"><span class="fa fa-angle-up"></span></a></b></div><div class="panel-collapse in" id="queue"><div class="panel-body">
<blockquote>
<p>[!note|style:flat]
<strong>&#x90FD;&#x7528;&#x56DE;&#x6EAF;&#x7B97;&#x6CD5;&#xFF0C;&#x65F6;&#x95F4;&#x80AF;&#x5B9A;&#x4E0D;&#x8FBE;&#x6807;&#xFF0C;&#x653E;&#x5FC3;&#x5927;&#x80C6;&#x5FAA;&#x73AF;&#x5217;&#x6761;&#x4EF6;&#x3002;</strong></p>
</blockquote>
<pre class="language-"><code class="lang-cpp"><span class="token keyword">bool</span> <span class="token function">isOk</span><span class="token punctuation">(</span>vector<span class="token operator">&lt;</span>string<span class="token operator">&gt;</span> <span class="token operator">&amp;</span> bord<span class="token punctuation">,</span><span class="token keyword">int</span> row <span class="token punctuation">,</span><span class="token keyword">int</span> col<span class="token punctuation">)</span><span class="token punctuation">{</span>

    <span class="token comment">// &#x68C0;&#x67E5;&#x884C;</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> bord<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span>bord<span class="token punctuation">[</span>row<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token string">&apos;Q&apos;</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// &#x68C0;&#x67E5;&#x5217;</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> bord<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span>bord<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>col<span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token string">&apos;Q&apos;</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">int</span> states<span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
    states<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> row <span class="token operator">-</span> <span class="token number">1</span> <span class="token operator">&gt;=</span> <span class="token number">0</span><span class="token punctuation">;</span>
    states<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> row <span class="token operator">+</span> <span class="token number">1</span> <span class="token operator">&lt;</span> bord<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    states<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token operator">=</span> col <span class="token operator">-</span> <span class="token number">1</span> <span class="token operator">&gt;=</span> <span class="token number">0</span><span class="token punctuation">;</span>
    states<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> col <span class="token operator">+</span> <span class="token number">1</span> <span class="token operator">&lt;</span> bord<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// &#x4E0A;&#x89D2;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>states<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>states<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span> bord<span class="token punctuation">[</span>row<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span>col<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token string">&apos;Q&apos;</span><span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>states<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span> bord<span class="token punctuation">[</span>row<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span>col<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token string">&apos;Q&apos;</span><span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span>states<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>states<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span> bord<span class="token punctuation">[</span>row<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span>col<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token string">&apos;Q&apos;</span><span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>states<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span> bord<span class="token punctuation">[</span>row<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span>col<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token string">&apos;Q&apos;</span><span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">void</span> <span class="token function">solveNQueens</span><span class="token punctuation">(</span>vector<span class="token operator">&lt;</span>string<span class="token operator">&gt;</span> <span class="token operator">&amp;</span> bord<span class="token punctuation">,</span><span class="token keyword">int</span> count<span class="token punctuation">)</span><span class="token punctuation">{</span>

    <span class="token keyword">if</span><span class="token punctuation">(</span>count <span class="token operator">==</span> bord<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">for</span><span class="token punctuation">(</span>string str<span class="token operator">:</span>bord<span class="token punctuation">)</span><span class="token punctuation">{</span>
            cout <span class="token operator">&lt;&lt;</span> str <span class="token operator">&lt;&lt;</span> endl<span class="token punctuation">;</span>
        <span class="token punctuation">}</span> 
        <span class="token keyword">return</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> bord<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span>

        <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">isOk</span><span class="token punctuation">(</span>bord<span class="token punctuation">,</span>count<span class="token punctuation">,</span>i<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            bord<span class="token punctuation">[</span>count<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">&apos;Q&apos;</span><span class="token punctuation">;</span>

            <span class="token function">solveNQueens</span><span class="token punctuation">(</span>bord<span class="token punctuation">,</span>count<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

            bord<span class="token punctuation">[</span>count<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">&apos;.&apos;</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
</div></div></div></sec>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="dynamicProgram_withNum.html" class="navigation navigation-prev " aria-label="Previous page: 十四、动态规划">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="monotoneStack_withNum.html" class="navigation navigation-next " aria-label="Next page: 十六、单调栈">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"十五、回溯算法","level":"2.1.15","depth":2,"next":{"title":"十六、单调栈","level":"2.1.16","depth":2,"path":"cpp/chapter/monotoneStack_withNum.md","ref":"cpp/chapter/monotoneStack_withNum.md","articles":[]},"previous":{"title":"十四、动态规划","level":"2.1.14","depth":2,"path":"cpp/chapter/dynamicProgram_withNum.md","ref":"cpp/chapter/dynamicProgram_withNum.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-lunr","-search","search-pro-fixed","expandable-chapters-small","ancre-navigation","hide-element","code","splitter","insert-logo-link-website","-sharing","sharing-plus","prism","-highlight","flexible-alerts","lightbox","pageview-count","auto-scroll-table","katex-pro","favicon-absolute","sectionx","terminull-light","livereload"],"styles":{"website":"styles/website.css","ebook":"styles/ebook.css","pdf":"styles/pdf.css","mobi":"styles/mobi.css","epub":"styles/epub.css"},"pluginsConfig":{"prism":{"css":["prismjs/themes/prism-tomorrow.css"],"lang":{"flow":"typescript"}},"livereload":{},"splitter":{},"sharing-plus":{"qq":false,"all":["facebook","google","twitter","instapaper","linkedin","pocket","stumbleupon"],"douban":false,"facebook":true,"weibo":false,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":true,"messenger":false,"line":false,"vk":false,"pocket":true,"google":false,"viber":false,"stumbleupon":false,"qzone":false,"linkedin":false},"auto-scroll-table":{},"code":{"copyButtons":true},"terminull-light":{},"search-pro-fixed":{},"favicon-absolute":{"favicon":"image/ico.png","appleTouchIconPrecomposed152":"image/ico.png","appleTouchIconMore":{},"appleTouchIconPrecomposedMore":{}},"hide-element":{"elements":[".gitbook-link"]},"katex-pro":{},"fontsettings":{"theme":"white","family":"sans","size":2},"sectionx":{"tag":"b"},"lightbox":{"jquery":true,"sameUuid":false},"insert-logo-link-website":{"url":"image/logo.png","link":"https://space.bilibili.com/27206875","target":"_blank","style":"height: 230px"},"pageview-count":{},"ancre-navigation":{"showLevel":true,"associatedWithSummary":true,"mode":"float","printLog":false,"multipleH1":true,"float":{"showLevelIcon":false,"level1Icon":"","level2Icon":"","level3Icon":""},"pageTop":{"showLevelIcon":false,"level1Icon":"","level2Icon":"","level3Icon":""}},"expandable-chapters-small":{},"flexible-alerts":{"style":"callout","note":{"label":"Note","icon":"fa fa-info-circle","className":"info"},"tip":{"label":"Tip","icon":"fa fa-lightbulb-o","className":"tip"},"warning":{"label":"Warning","icon":"fa fa-exclamation-triangle","className":"warning"},"danger":{"label":"Attention","icon":"fa fa-ban","className":"danger"}},"sharing":{"qq":true,"all":["douban","facebook","google","instapaper","linkedin","twitter","weibo","messenger","qq","qzone","viber","whatsapp"],"douban":false,"facebook":false,"weibo":true,"whatsapp":false,"twitter":false,"pocket":false,"google":true,"qzone":false},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"海拉尔的三角","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"计算机笔记","links":{"sidebar":{"Github":"https://github.com/spite-triangle"}},"gitbook":"*"},"file":{"path":"cpp/chapter/backtracking_withNum.md","mtime":"2021-09-14T15:43:12.138Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-09-14T15:43:23.052Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro-fixed/jquery.mark.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro-fixed/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-hide-element/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-code/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-insert-logo-link-website/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing-plus/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-flexible-alerts/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lightbox/js/lightbox.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-pageview-count/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-auto-scroll-table/plugin.js"></script>
        
    
        
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sectionx/sectionx.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-terminull-light/terminull.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

